#!/usr/bin/env bash

local_dir=/home/hanxiao/wspace/docker_src/sfc-src/build_ninja/deploy/
# remote_dir=linaro@192.168.22.3:/home/linaro/opt/aiforce/bin/   # 上地测试
remote_dir=linaro@192.168.1.247:/home/linaro/opt/aiforce/bin/ # 小汤山1404上车测试 192.168.1.148
# remote_dir=linaro@192.168.0.104:/home/linaro/opt/aiforce/bin/ #  192.168.0.103 哈尔滨工区
# remote_dir=linaro@192.168.1.111:/home/linaro/opt/aiforce/bin/ #  1009 农校2204
# remote_dir=linaro@192.168.1.167:/home/linaro/opt/aiforce/bin/ #  上地
remote_config=linaro@192.168.22.3:/home/linaro/opt/aiforce/config/ 

# 可以公网使用
# rsync -av -e 'ssh -p 6001' ./1 linaro@101.43.219.79:/home/linaro/opt/
# ssh -p 6001 linaro@101.43.219.79

# remote_dir=/home/hanxiao/Remote_Files/tt/  # for testing

# 无参数默默认同步所有可执行文件
if [ $# -eq 0 ]; then
  rsync -av --include='control_auto' \
  --include='decision_auto' \
  --include='control_semi_auto' \
  --include='decision_semi_auto' \
  --exclude='*' \
  $local_dir \
  $remote_dir
  echo "Synced all executables to remote directory."
  exit 0
fi

# 检查参数是否为 -n 并获取可执行文件名
if [ "$1" == "-n" ]; then
  if [ -z "$2" ]; then
    echo "Error: -n 需要执行指定一个特定的可执行文件名."
    exit 1
  fi
  exe_name=$2
  shift 2
  rsync -av --include="$exe_name" --exclude='*' $local_dir $remote_dir
  echo "已同步文件 '$exe_name' 到远程目录"
  exit 0
fi

# 参数为 -c 则同步配置文件
if [ "$1" == "-c" ]; then
  rsync -av --include="control_l2.toml" \
            --include="control_l4.toml" \
            --include="decision_l2.toml" \
            --include="decision_l4.toml" \
            --exclude='*' \
            $local_dir $remote_config
  echo "已同步toml文件到远程目录"
  exit 0
fi

# 根据参数决定同步的文件
if [ $# -ne 1 ]; then
  echo "Usage: $0 {l2c|l2d|l4c|l4d|l2|l4|cc|dd|all}"
  exit 1
fi

case "$1" in
  l2c)
    files_to_sync="control_semi_auto"
    ;;
  l2d)
    files_to_sync="decision_semi_auto"
    ;;
  l4c)
    files_to_sync="control_auto"
    ;;
  l4d)
    files_to_sync="decision_auto"
    ;;
  l2)
    files_to_sync="control_semi_auto decision_semi_auto"
    ;;
  l4)
    files_to_sync="control_auto decision_auto"
    ;;
  cc)
    files_to_sync="control_auto control_semi_auto"
    ;;
  dd)
    files_to_sync="decision_auto decision_semi_auto"
    ;;
  all)
    files_to_sync="control_auto decision_auto control_semi_auto decision_semi_auto"
    ;;
  *)
    echo "Invalid argument: $1"
    echo "Valid arguments are: l2c l2d l4c l4d l2 l4 cc dd all"
    exit 1
    ;;
esac

# 构建rsync命令
for file in $files_to_sync; do
  rsync_command+=" --include='${file}'"
done

rsync_command+=" --exclude='*'"

run_cmd="rsync -av $rsync_command $local_dir $remote_dir"
echo "Executing: $run_cmd"
eval $run_cmd